package com.zxb.base.cms;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxb.base.cms.vo.AddAnnVo;
import com.zxb.base.cms.vo.UpdateAnnVo;
import com.zxb.base.system.service.AttachService;
import com.zxb.core.common.A;
import com.zxb.core.common.C;
import com.zxb.core.common.R;
import com.zxb.core.dao.cms.CmsAnn;
import com.zxb.core.dao.cms.CmsAnnDao;
import com.zxb.core.dao.system.SysAttach;
import com.zxb.core.dao.system.SysBigStr;
import com.zxb.core.dao.system.SysBigStrDao;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author Zxb
 */
@RestController
@RequiredArgsConstructor
public class AnnController {
    private static final String TABLE_NAME = "cms_ann";
    private final CmsAnnDao annDao;
    private final SysBigStrDao bigStrDao;
    private final AttachService attachService;

    @PostMapping("/ann/add")
    @Transactional(rollbackFor = Exception.class)
    public R add(@RequestBody @Valid AddAnnVo param) {
        if (annDao.exists(
                new QueryWrapper<CmsAnn>()
                        .lambda()
                        .eq(CmsAnn::getTitle, param.getTitle())
        )) {
            return R.error("公告标题已经存在");
        }
        SysBigStr bigStr = new SysBigStr();
        bigStr.setContent(param.getContent());
        bigStrDao.save(bigStr);

        CmsAnn obj = new CmsAnn();
        obj.setContentId(bigStr.getId());
        BeanUtil.copyProperties(param, obj, C.IGNORED_PROPERTIES);
        annDao.save(obj);

        attachService.bindAttach(TABLE_NAME, String.valueOf(obj.getId()), param.getFileVoList());
        return R.success();
    }

    @PostMapping("/ann/update")
    @Transactional(rollbackFor = Exception.class)
    public R update(@RequestBody @Valid UpdateAnnVo obj) {
        CmsAnn old = annDao.getById(obj.getId());
        A.isInDatabase(old, "公告");
        if (annDao.exists(
                new QueryWrapper<CmsAnn>()
                        .lambda()
                        .eq(CmsAnn::getTitle, obj.getTitle())
                        .ne(CmsAnn::getId, old.getId())
        )) {
            return R.error("公告标题已经存在");
        }

        bigStrDao.removeById(old.getContentId());

        SysBigStr bigStr = new SysBigStr();
        bigStr.setContent(obj.getContent());
        bigStrDao.save(bigStr);

        old.setContentId(bigStr.getId());
        BeanUtil.copyProperties(obj, old, C.IGNORED_PROPERTIES);
        annDao.updateById(old);

        attachService.resetAttach(TABLE_NAME, String.valueOf(obj.getId()), obj.getFileVoList());
        return R.success();
    }

    @PostMapping("/ann/del")
    public R del(@RequestBody List<Long> ids) {
        annDao.removeBatchByIds(ids);
        return R.success();
    }

    @GetMapping("/ann/page")
    public R page(String searchStr,
                  String title,
                  String author,
                  String isPublish,
                  String isTop,
                  Integer page,
                  Integer pageSize) {
        IPage<CmsAnn> pageParam = new Page<>(page, pageSize);
        LambdaQueryWrapper<CmsAnn> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(searchStr)) {
            lambdaQueryWrapper.like(CmsAnn::getSearchStr, "%" + searchStr + "%");
        }
        if (StrUtil.isNotBlank(title)) {
            lambdaQueryWrapper.like(CmsAnn::getTitle, "%" + title + "%");
        }
        if (StrUtil.isNotBlank(author)) {
            lambdaQueryWrapper.like(CmsAnn::getAuthor, "%" + author + "%");
        }
        if (StrUtil.isNotBlank(isPublish)) {
            lambdaQueryWrapper.eq(CmsAnn::getIsPublish, "true".equals(isPublish));
        }
        if (StrUtil.isNotBlank(isTop)) {
            lambdaQueryWrapper.eq(CmsAnn::getIsTop, "true".equals(isTop));
        }
        IPage<CmsAnn> result = annDao.page(pageParam, lambdaQueryWrapper);
        return R.success(result);
    }

    @GetMapping("/ann/info")
    public R info(Long annId) {
        CmsAnn ann = annDao.getById(annId);
        SysBigStr content = bigStrDao.getById(ann.getContentId());
        List<SysAttach> list = attachService.listAttaches(TABLE_NAME, String.valueOf(annId));
        return R.success(Arrays.asList(ann, content, list));
    }
}
